package my.shadowsocks.common.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author fengbo
 * @date 2018/9/28
 */
public class Config {

    private static final Logger log = LoggerFactory.getLogger(Config.class);

    private final Properties prop;

    private Config() {
        prop = new Properties();
    }
    private static class Single {
        private static final Config INSTANCE = new Config();
    }
    public static Config getInstance() {
        return Single.INSTANCE;
    }

    public String getStringValue(String key) {
        return prop.get(key).toString();
    }

    public int getIntValue(String key) {
        return Integer.parseInt(getStringValue(key));
    }

    public int getIntValue(String key, int defaultVal) {
        String val = getStringValue(key);
        if (val != null && val.trim().length() > 0) {
            return Integer.parseInt(val);
        }
        return defaultVal;
    }

    public void init(InputStream is) {
        try {
            prop.load(is);
        } catch (IOException e) {
            log.error("Init Config Error!", e);
            throw new RuntimeException(e);
        }
    }
}
